<?php 
namespace app\app\controller\v1;;
use app\app\model\Goods;
use app\app\model\Store;
use app\app\util\OnlineUtils;
use think\Request;
use think\Cache;

class GoodsController extends BaseController{

	/**
	 * [index 商户商品]
	 * @return [type] [description]
	 */
	public function index()
	{
		$user_id = OnlineUtils::getUserIdBySid();
        //清除购物车
        // $name = config('shoppingcart').$user_id.config('store').input('get.store_id');
        // Cache::rm($name);		
		//取出购物车数据
		$shoppingcart = Cache::get(config('shoppingcart').$user_id.config('store').input('get.store_id'));
		if( $shoppingcart == false ){
			$shoppingcart = [];
		}
		// return jsonReturn(200,'success',$shoppingcart['pocket']);
		//store_id
		$goods = new Goods();
		$goods->store_id = input('get.store_id');
		$return = $goods->getAll($shoppingcart);
		//取出商家数据
		$store  = new Store();
		$stores = $store->getOne(input('get.store_id'));
		return jsonReturn(200,'success',['goods_list'=>$return,'store'=>$stores]);
	}

	/**
	 * [addGoods description]
	 */
	public function addGoods()
	{
		$user_id = OnlineUtils::getUserIdBySid();
		$data    = input('post.');
		//验证
		$result = $this->Validate($data,'Goods');
		if( true !== $result ){
			return jsonReturn(402,$result);
		}
		//实例化
		$goods = new Goods();
		//判断是否重名 
		$result = $goods->getOne($data['name']);
		if( $result ) return jsonReturn(402,'商品名已存在');
		$data['create_time'] = time();
		$goods->save($data);
		return jsonReturn(200,'success',$goods->id);
	}

}